package com.hotacorp.opencloud.userservice.service.impl;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hotacorp.opencloud.userservice.dao.SysMenuDao;
import com.hotacorp.opencloud.userservice.dao.SysRoleMenuDao;
import com.hotacorp.opencloud.userservice.dao.SysUserRoleDao;
import com.hotacorp.opencloud.userservice.entity.SysMenuEntity;
import com.hotacorp.opencloud.userservice.entity.SysRoleMenuEntity;
import com.hotacorp.opencloud.userservice.entity.SysUserRoleEntity;
import com.hotacorp.opencloud.userservice.service.SysMenuService;

/**
 * 菜单管理接口实现
 * @author lwg
 *
 */
@Service("sysMenuService")
public class SysMenuServiceImpl extends ServiceImpl<SysMenuDao, SysMenuEntity> implements SysMenuService {
	@Autowired
	private SysUserRoleDao sysUserRoleDao;
	@Autowired
	private SysRoleMenuDao sysRoleMenuDao;

	@Override
	public List<SysMenuEntity> getUserMenuList(Long userid, String username) {
		List<SysMenuEntity> allmenus = Collections.emptyList();
		if (userid == 1) {//系统超级管理员
			allmenus = this.baseMapper.selectList((new QueryWrapper<SysMenuEntity>()).in("type", Arrays.asList(0, 1)).orderByAsc("order_num"));
		} else if ("admin".equals(username)) {//租户管理员
			allmenus = this.baseMapper.selectList((new QueryWrapper<SysMenuEntity>()).eq("adminused", 0).in("type", Arrays.asList(0, 1)).orderByAsc("order_num"));
		} else {
			List<SysUserRoleEntity> userroles = sysUserRoleDao.selectList((new QueryWrapper<SysUserRoleEntity>()).eq("user_id", userid));
			Set<Long> roleidSet = new HashSet<>();
			userroles.forEach(userrole->{roleidSet.add(userrole.getRoleId());});
			if (roleidSet.size() > 0) {
				List<SysRoleMenuEntity> rolemenus = sysRoleMenuDao.selectList((new QueryWrapper<SysRoleMenuEntity>()).in("role_id", roleidSet));
				Set<Long> menuidSet = new HashSet<>();
				rolemenus.forEach(rolemenu->{menuidSet.add(rolemenu.getMenuId());});
				if (menuidSet.size() > 0) {
					allmenus = this.baseMapper.selectList((new QueryWrapper<SysMenuEntity>()).in("menu_id", menuidSet).eq("adminused", 0).in("type", Arrays.asList(0, 1)).orderByAsc("order_num"));
				}
			}
		}
		List<SysMenuEntity> menus = allmenus.stream().filter(new Predicate<SysMenuEntity>() {
			@Override
			public boolean test(SysMenuEntity t) {
				return t.getParentId() == 0;
			}
		}).collect(Collectors.toList());
		orderMenu(menus, allmenus);
		return menus;
	}

	private void orderMenu(List<SysMenuEntity> menus, List<SysMenuEntity> allmenus) {
		menus.forEach(menu->{
			if (menu.getType() == 0) {
				List<SysMenuEntity> menus1 = allmenus.stream().filter(new Predicate<SysMenuEntity>() {
					@Override
					public boolean test(SysMenuEntity t) {
						return t.getParentId().longValue() == menu.getMenuId().longValue();
					}
				}).collect(Collectors.toList());
				menu.setList(menus1);
				orderMenu(menus1, allmenus);
			}
		});
	}

	@Override
	public List<SysMenuEntity> queryNotButtonList() {
		return baseMapper.selectList((new QueryWrapper<SysMenuEntity>()).in("type", Arrays.asList(0, 1)).orderByAsc("order_num"));
	}

	@Override
	public List<SysMenuEntity> listAll() {
		return baseMapper.selectList((new QueryWrapper<SysMenuEntity>()).orderByAsc("order_num"));
	}

	@Override
	@Transactional
	public int delete(Long menuId) {
		int aaa = baseMapper.selectCount((new QueryWrapper<SysMenuEntity>()).eq("parent_id", menuId));
		if (aaa > 0) return 1;
		sysRoleMenuDao.delete((new QueryWrapper<SysRoleMenuEntity>()).eq("menu_id", menuId));
		baseMapper.deleteById(menuId);
		return 0;
	}
}